# name: proc mean with class for pandas equivalent to SAS
# key: proc_means_equiv_class@pandas
# contributor: Shuguang Sun
# --
def proc_means_equiv_w_class(ds, analysis_vars, group_var):
    levels = pd.unique(ds[group_var])
    df = pd.DataFrame()
    for i in range(0,len(levels)):
        temp=ds[ds[group_var]==levels[i]]
        temp2=temp[analysis_vars.split(" ")].describe().transpose()
        temp2["level"]=levels[i]
        temp2["nmiss"]=temp.isnull().sum()
        temp2.reset_index(inplace=True)
        df = df.append(temp2, ignore_index=True)
    df.rename(columns={"25%":"p25", "75%":"p75", "50%": "median", "count":"n", "index":"var"}, inplace=True)
    return df[['level','var','nmiss','n','mean','median','std','min','max','p25','p75']]

analysis = "${1:height weight}"
group = "${2:class}"

print(proc_means_equiv_w_class(df,analysis,group_var))
